{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 文本分类"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TF-IDF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from nltk.text import TextCollection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "That的TF-IDF值为：0.02181644599700369\n"
     ]
    }
   ],
   "source": [
    "text1 = 'I like the movie so much '\n",
    "text2 = 'That is a good movie '\n",
    "text3 = 'This is a great one '\n",
    "text4 = 'That is a really bad movie '\n",
    "text5 = 'This is a terrible movie'\n",
    "\n",
    "# 构建TextCollection对象\n",
    "tc = TextCollection([text1, text2, text3, \n",
    "                        text4, text5])\n",
    "new_text = 'That one is a good movie. This is so good!'\n",
    "word = 'That'\n",
    "tf_idf_val = tc.tf_idf(word, new_text)\n",
    "print('{}的TF-IDF值为：{}'.format(word, tf_idf_val))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
